package com.ai.study.dashscope.controller.chatClient;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * @Author chenwei
 * @Date 2025/5/27 10:11
 */
@RestController
@RequestMapping("/dashscope/client")
@RequiredArgsConstructor
@Tag(name = "chatClient测试")
public class DashScopeChatClientController {

    private static final String DEFAULT_PROMPT = "你好，介绍下你自己！";

    @Autowired
    @Qualifier("dashScopeChatClient")
    private final ChatClient dashScopeChatClient;

    // public DashScopeChatController(DashScopeChatModel chatModel) {
    //
    //     // 构造时，可以设置 ChatClient 的参数
    //     // {@link org.springframework.ai.chat.client.ChatClient};
    //     this.dashScopeChatClient = ChatClient.builder(chatModel)
    //             // 实现 Logger 的 Advisor
    //             .defaultAdvisors(
    //                     new SimpleLoggerAdvisor()
    //             )
    //             // 设置 ChatClient 中 ChatModel 的 Options 参数
    //             .defaultOptions(
    //                     DashScopeChatOptions.builder()
    //                             .withTopP(0.7)
    //                             .build()
    //             )
    //             .build();
    // }

    // 也可以使用如下的方式注入 ChatClient
    // public DashScopeChatClientController(ChatClient.Builder chatClientBuilder) {
    //
    //  	this.dashScopeChatClient = chatClientBuilder.build();
    // }

    /**
     * ChatClient 简单调用
     */
    @GetMapping("/simple/chat")
    @Operation(summary = "chatClient call 简单调用")
    public String simpleChat(@RequestParam(value = "prompt", defaultValue = DEFAULT_PROMPT) String prompt) {

        return dashScopeChatClient.prompt(prompt).call().content();
    }

    /**
     * ChatClient 流式调用
     */
    @GetMapping("/stream/chat")
    @Operation(summary = "chatClient stream 简单调用")
    public Flux<String> streamChat(@RequestParam(value = "prompt", defaultValue = DEFAULT_PROMPT) String prompt,
                                   HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        return dashScopeChatClient.prompt(prompt).stream().content();
    }

    /**
     * ChatResponse 响应元数据
     * @param prompt
     * @return
     */
    @GetMapping("/chat/response")
    @Operation(summary = "chatClient stream 带 Advisor 调用")
    public ChatResponse chatWithResponse(@RequestParam(value = "prompt", defaultValue = DEFAULT_PROMPT) String prompt) {
        return dashScopeChatClient.prompt()
                .user(prompt)
                .call()
                .chatResponse();
    }

    /**
     * ChatResponse 返回实体类
     * 用于返回一个预先定义好的实体类类型响应
     * @param prompt
     * @return
     */
    @GetMapping("/chat/withentity")
    @Operation(summary = "chatClient stream 带 Advisor 调用")
    public ActorFilms chatWithEntity(@RequestParam(value = "prompt", defaultValue = DEFAULT_PROMPT) String prompt) {
        return dashScopeChatClient.prompt()
                .user(prompt)
                .call()
                .entity(ActorFilms.class);
    }

    record ActorFilms(String actor, List<String> movies) {
    }
}



























